/**
 * Created by senlin.gu on 18/08/07
 * 同天地之规量兮，齐日月之晖光。
 * 永贵尊而无极兮，等君寿于东皇。
 */
import {functionProfileData, functionProfileTabs} from 'containers/setting/function-profile/function-profile.data.js'

const ignoreList = [
  'hotel.itinerary.enable',
  'ca.travel.flight.disabled',
  'ca.travel.train.disabled',
  'approval.start.float.days',
  'approval.end.float.days',
  'ca.travel.other.disabled',
  'ca.travel.remark.disabled',
  //'application.close.enabled', bug 11633 说是遗漏
  'application.close.restart.closeDay', //bug 11418 要求删除
  'application.property.manage.type',
  'application.property.control.fields',
  'application.participants.import.scope',
  'application.change.enable',
  'travel.allowance.web.disabled',
  'Web.InvCon.InvCod.Display', //bug11479要求删除,
  'ca.loan.opt.finance.audit.disabled',  //bug12144要求删除,
  'ea.opt.withdraw.disabled',
  'ca.opt.withdraw.disabled',
  'er.opt.withdraw.disabled',
  'bill.approved.withdraw',
];

const onlyCompany = [
  'didi.invoice.check.enable',
];

const radios = {
  "finance.pay.node": {
    "defaultValue": 102,
    "list": [
      {
        "value": 101,
        "title": "审核通过可付款"
      },
      {
        "value": 102,
        "title": "开票通过可付款"
      }
    ]
  },
  "All.BusinessCardAmount.FeeAmount": {
    "defaultValue": 3,
    "list": [
      {
        "value": 3,
        "title": "小于等于"
      },
      {
        "value": 2,
        "title": "等于"
      },
      {
        "value": 1,
        "title": "不校验"
      }
    ]
  },
  "All.FeeAllowance.equal": {
    "defaultValue": 1,
    "list": [
      {
        "value": 1,
        "title": "小于等于"
      },
      {
        "value": 2,
        "title": "等于"
      },
      {
        "value": 3,
        "title": "不校验"
      }
    ]
  },
  "approval.rule.self.skip": {
    "defaultValue": 5003,
    "list": [
      {
        "value": 5001,
        "title": "跳过"
      },
      {
        "value": 5002,
        "title": "不跳过"
      },
      {
        "value": 5003,
        "title": "上级部门经理审批"
      }
    ]
  },
  "InvoiceControl.InvoiceAmount.FeeAmount.ALL.Equal": {
    "defaultValue": 0,
    "list": [
      {
        "value": 0,
        "title": "小于等于"
      },
      {
        "value": 1,
        "title": "不校验"
      },
      {
        "value": 2,
        "title": "等于"
      }
    ]
  },
  "approval.req.mail": {
    "defaultValue": 1003,
    "list": [
      {
        "value": 1001,
        "title": "全部启用"
      },
      {
        "value": 1003,
        "title": "不启用"
      },
      {
        "value": 1002,
        "title": "仅对加签人启用"
      }
    ]
  },
  "paste.invoice.copies": {
    "defaultValue": "1",
    "list": [
      {
        "value": "1",
        "title": "1"
      },
      {
        "value": "4",
        "title": "4"
      }
    ]
  },
  "advance.report.auto.audit": {
    "defaultValue": 1,
    "list": [
      {
        "value": 1,
        "title": "不开启自动审核"
      },
      {
        "value": 2,
        "title": "开启自动审核"
      }
    ]
  }
};

const onlyTenant = [
  //'pay.choice.banknum',
  'branch.workflow.enabled'
];

function filterProfileList(profileCollectList) {
  for (let key in profileCollectList) {
    let profileValue = profileCollectList[key];

    if (profileValue.owner == 'web&app') {
      profileValue.owner = 'all';
    }

    if (profileValue.enabled == '否') {
      profileValue.enabled = 'Y';
    } else {
      profileValue.enabled = 'N';
    }
    if (profileValue.customer == '是') {
      profileValue.customer = 'Y';
    } else {
      profileValue.customer = 'N';
    }
    if (profileValue.deliver == '是') {
      profileValue.deliver = 'Y';
    } else {
      profileValue.deliver = 'N';
    }

    if(profileValue.code === 'er.split'){
      profileValue.readOnly = true;
    }
  }
}

function deleteNoUseProfile(type, profileCollectList) {

  ignoreList.forEach(function (data) {
    delete profileCollectList[data];
  });

  if (type == 'group') {
    onlyCompany.forEach(function (data) {
      delete profileCollectList[data];
    });
  } else if (type == 'company') {
    onlyTenant.forEach(function (data) {
      delete profileCollectList[data];
    });
  }
}

export default {

  getFunctionProfileTabs: function () {
    return functionProfileTabs;
  },

  getBaseProfileTable: function (type) {
    let baseProfileTable = JSON.parse(JSON.stringify(functionProfileData));
    filterProfileList(baseProfileTable);
    deleteNoUseProfile(type, baseProfileTable);
    return baseProfileTable;
  },

  processSubmitProfile: function (systemProfile, screenProfile) {

    var profileName = '';
    var invalidJSONFlag = false;
    var isNullFlag = false;
    var keepGoing = true;
    let profileDetail = '';
    let result = {'status': 'S', 'errorMsg': ''};

    screenProfile.forEach(function (item) {

      if (keepGoing && (item.category === 'basic' ||
        item.category === 'closed' && item.enabledFlag)) {

        profileName = item.description;

        //console.log('item ' + angular.toJson(item))
        if (item.componentType == 'object') {
          try {
            if (!item.value || item.value == '') {
              isNullFlag = true;
              keepGoing = false;
            }

            systemProfile[item.code] = JSON.parse(item.value)
          } catch (e) {
            invalidJSONFlag = true;
            keepGoing = false;
          }
        } else if (item.componentType == 'number') {
          try {

            if (!item.value || item.value == '') {
              isNullFlag = true;
              keepGoing = false;
            }

            systemProfile[item.code] = parseInt(item.value);
          } catch (e) {
            invalidJSONFlag = true;
            keepGoing = false;
          }
        } else if (item.componentType == 'string') {
          if ((!item.value || item.value == '') && item.code != 'approval.req.mail.sso.url') {
            isNullFlag = true;
            keepGoing = false;
          }

          systemProfile[item.code] = item.value;

        } else {
          systemProfile[item.code] = item.value
        }

      }
    });

    if (invalidJSONFlag) {
      result.status = 'E';
      result.errorMsg = 'Profile(' + profileName + ')中含有不合法的JSON';
      return result;
    } else if (isNullFlag) {
      result.status = 'E';
      result.errorMsg = 'Profile(' + profileName + ')的值不能为空';
      return result;
    } else {
      result.profileDetail = systemProfile;
      return result;
    }
  },

  analysisProfile(systemProfile, baseProfile) {

    let baseProfileTable = JSON.parse(JSON.stringify(baseProfile));
    let functionProfileArray = [];

    //处理已经登记过的Profile
    for (let profile in baseProfileTable) {
      let profileValue = baseProfileTable[profile];
      let profileCategory;
      let value;
      let lovList;

      if (systemProfile[profile] === undefined) {

        profileCategory = 'closed';
        if (profileValue.componentType === 'boolean') {
          value = false;
        } else if (profileValue.componentType === 'string') {
          value = '';
        } else {
          value = null;
        }

        if (profileValue.componentType == 'radio') {
          let radioMap = radios[profile];
          value = radioMap.defaultValue;
          lovList = radioMap.list;
        }

        if (profileValue.componentType == 'object' && value != null) {
          value = JSON.stringify(value)
        }

        functionProfileArray.push({
          "code": profile,
          "description": profileValue.description,
          "owner": profileValue.owner,
          "componentType": profileValue.componentType,
          "type": profileValue.type,
          "enabled": profileValue.enabled,
          "customer": profileValue.customer,
          "deliver": profileValue.type,
          "active": profileValue.active,
          "readOnly": profileValue.readOnly,
          "inactive": profileValue.inactive,
          "value": value,
          "lovList": lovList,
          "category": profileCategory
        });

      } else {
        profileCategory = 'basic';
        value = systemProfile[profile];
        if (profileValue.componentType == 'object' && value != null) {
          value = JSON.stringify(value)
        }

        if (profileValue.componentType == 'radio') {
          let radioMap = radios[profile];
          lovList = radioMap.list;
        }

        functionProfileArray.push({
          "code": profile,
          "description": profileValue.description,
          "owner": profileValue.owner,
          "componentType": profileValue.componentType,
          "type": profileValue.type,
          "enabled": profileValue.enabled,
          "customer": profileValue.customer,
          "deliver": profileValue.type,
          "active": profileValue.active,
          "readOnly": profileValue.readOnly,
          "inactive": profileValue.inactive,
          "value": value,
          "lovList": lovList,
          "category": profileCategory
        });
      }
    }

    //处理未登记过的Profile
    for (let obj in systemProfile) {
      if (baseProfileTable[obj] == undefined) {
        functionProfileArray.push({
          "code": obj,
          "description": '',
          "ower": '',
          "componentType": 'notFound',
          "type": 'Unidentified',
          "enabled": 'Y',
          "customer": 'Y',
          "deliver": '',
          "value": systemProfile[obj],
          "category": ''
        });
      }
    }


    return functionProfileArray;
  }
}
